﻿using Google.Analytics.BusinessLogic.IDataAccess;
using Google.Analytics.Model;
using Google.Analytics.Model.Feeds;
using Google.Analytics.Model.Queries;
using Google.Analytics.RemoteAccess.API;

namespace Google.Analytics.RemoteAccess
{
    /// <summary>
    /// Provides a Data Access class for account and feed data
    /// </summary>
    public class AnalyticsDa : IAnalyticsDa
    {
        /// <summary>
        /// Authenticates user and creates <see cref="AuthState"/> object with <c>AuthenticationToken</c>
        /// </summary>
        /// <param name="state">authentication state</param>
        /// <returns><see cref="AuthState"/> object with authenticated token</returns>
        public AuthState Authenticate(AuthState state)
        {
            var api = new AnalyticsAPI(state);
            return api.ForceAuthentication();
        }

        /// <summary>
        /// Retreive <see cref="AccountFeed"/> data from Google Analytics
        /// </summary>
        /// <param name="query">account query</param>
        /// <returns></returns>
        public AccountFeed GetAccountFeed(AccountQuery query)
        {
            //create Analytics API, process the query
            var api = new AnalyticsAPI(query.AuthenticationState);
            AccountFeed feed = api.GetAccounts(query);
            //Cache.Set(query, feed);
            return feed;
        }

        /// <summary>
        /// Retrieve <see cref="DataFeed"/> data from Google Analytics
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public DataFeed GetDataFeed(DataQuery query)
        {
            var api = new AnalyticsAPI(query.AuthenticationState);
            DataFeed feed = api.GetData(query);
            //Cache.Set(query, feed);
            return feed;
        }
    }
}
